package com.timmy.ble;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inuker.bluetooth.library.search.SearchResult;

import java.util.ArrayList;
import java.util.List;

/**
 * 蓝牙设备Adapter
 */

public class DeviceAdapter extends BaseAdapter {

    private Context mContext;
    private List<SearchResult> mResultList;

    public DeviceAdapter(Context context) {
        mContext = context;
        mResultList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        if (mResultList != null) {
            return mResultList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mResultList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addData(SearchResult searchResult) {
        if (mResultList.contains(searchResult)) {
            return;
        }
        mResultList.add(searchResult);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item, null);
        }

        TextView text = (TextView) convertView.findViewById(R.id.textview);

        SearchResult searchResult = mResultList.get(position);

        text.setText(searchResult.getName() + "\n" + searchResult.getAddress());
        return convertView;
    }
}
